﻿Public Class ActivitySchedulerControl
    Inherits WinFormExt.SchedulerForm

    Public Sub New()
        MyBase.New()
        Me.LabelQueryName = Nothing
        Me.SchedulerControl1.OptionsCustomization.AllowAppointmentResize = False
        Me.SchedulerControl1.OptionsCustomization.AllowAppointmentMultiSelect = False
        Me.SchedulerControl1.OptionsCustomization.AllowInplaceEditor = False
        AddHandler OnAppointmentlLoadMapping, AddressOf MyOnAppointmentlLoadMapping
    End Sub

    Protected Overrides Sub SchedulerControl_EditAppointmentFormShowing(sender As Object, e As AppointmentFormEventArgs)
        Dim apt As Appointment = e.Appointment
        ' Required to open the recurrence form via context menu.
        'Dim openRecurrenceForm As Boolean = apt.IsRecurring AndAlso SchedulerStorage1.Appointments.IsNewAppointment(apt)
        '' Create a custom form.
        aEditFormAppointment = New AppointmentEditForm(CType(sender, SchedulerControl), apt)

        aEditFormAppointment.GroupQueryName = "LOV_ProjectGroup"
        aEditFormAppointment.CategoryQueryName = "LOV_ProjectCategory"
        aEditFormAppointment.ClassQueryName = "LOV_ProjectClass"
        aEditFormAppointment.TypeQueryName = "LOV_ProjectType"


        If GroupID Is Nothing Then
            aEditFormAppointment.LockGroup = False
        Else
            aEditFormAppointment.LockGroup = True
        End If

        aEditFormAppointment.Update()
        Try
            ' Required for skins support.
            aEditFormAppointment.LookAndFeel.ParentLookAndFeel = SchedulerControl1.LookAndFeel

            e.DialogResult = aEditFormAppointment.ShowDialog()
            'SchedulerControl1.Refresh()
            e.Handled = True
        Finally
            aEditFormAppointment.Dispose()
        End Try
    End Sub




    Public Overrides Function InitData() As DialogResult
        Dim aParamFrm As MqParamForm
        Dim aExecQuery As DialogResult = DialogResult.OK
        Dim aMq As MetaQuery = MetaQuery.GetMetaQueryByName(Pl.QueryName)
        Dim reader As SqlClient.SqlDataReader = Nothing
        Dim aDataRow As DataRow

        Dim FormText As String


        '
        SchedulerStorage1.Appointments.Clear()
        SchedulerStorage1.Appointments.DataSource = Nothing
        If Pl.Dataset.Tables.Contains(Pl.MetaDataTable.TableName) Then
            Pl.Dataset.Tables.Remove(Pl.MetaDataTable)
        End If
        Pl.Dataset.Clear()
        Pl.MetaDataTable.Clear()
        '
        If aMq.CommandTypeID = CommonExt.Base.IdRepository.METALIST_METACOMMANDTYPE_SP AndAlso aMq.MetaParams.Rows.Count > 0 Then
            aParamFrm = New MqParamForm(aMq.QueryName)
            aExecQuery = aParamFrm.ShowDialog()
            'On dit OK pour la form de Parametre
            If aExecQuery = DialogResult.OK Then
                '
                aSqlcmd = aMq.InitQueryParams
                Dim param As SqlClient.SqlParameter
                FormText = "Schedule for <Group> <Class> <Categorie> <Type>"
                For Each row As DataRow In aMq.MetaParams.Rows
                    If aSqlcmd.Parameters.Contains(row("ColumnName").ToString) Then
                        param = aSqlcmd.Parameters(row("ColumnName").ToString)
                        If param IsNot Nothing AndAlso param.ParameterName = "@GroupId_FK" AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                            param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                            GroupID = CType(IIf(param.Value IsNot Nothing, Guid.Parse(param.Value.ToString), Nothing), Guid)
                            aDataRow = SystemBoxSDK.PL.CoreSDK.ListValues_PL.GetListValueByID(CType(GroupID, Guid))
                            If aDataRow IsNot Nothing Then
                                FormText = FormText.Replace("<Group>", aDataRow("Value_PRM") + ", ")
                            End If
                        ElseIf param IsNot Nothing AndAlso param.ParameterName = "@ClassId_FK" AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                            param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                            ClassID = CType(IIf(param.Value IsNot Nothing, Guid.Parse(param.Value.ToString), Nothing), Guid)
                            aDataRow = SystemBoxSDK.PL.CoreSDK.ListValues_PL.GetListValueByID(CType(ClassID, Guid))
                            If aDataRow IsNot Nothing Then
                                FormText = FormText.Replace("<Class>", aDataRow("Value_PRM") + ", ")
                            End If

                        ElseIf param IsNot Nothing AndAlso param.ParameterName = "@CategoryId_FK" AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                            param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                            CategID = CType(IIf(param.Value IsNot Nothing, Guid.Parse(param.Value.ToString), Nothing), Guid)
                            aDataRow = SystemBoxSDK.PL.CoreSDK.ListValues_PL.GetListValueByID(CType(CategID, Guid))
                            If aDataRow IsNot Nothing Then
                                FormText = FormText.Replace("<Categorie>", aDataRow("Value_PRM") + ", ")
                            End If

                        ElseIf param IsNot Nothing AndAlso param.ParameterName = "@TypeId_FK" AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                            param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                            TypeID = CType(IIf(param.Value IsNot Nothing, Guid.Parse(param.Value.ToString), Nothing), Guid)
                            aDataRow = SystemBoxSDK.PL.CoreSDK.ListValues_PL.GetListValueByID(CType(TypeID, Guid))
                            If aDataRow IsNot Nothing Then
                                FormText = FormText.Replace("<Type>", aDataRow("Value_PRM") + ", ")
                            End If

                        End If
                        Me.Text = FormText
                    Else
                        MessageWindow.ShowMessage(New Exception(String.Format("Scheduler execution, SQL Parameter not found: {0}", row("ColumnName"))))
                    End If
                Next
                '
                'On Passe la sqcmd qui a les paramêtre de rentré
                Pl.MetaDataTable.GetMetaCommand()
                LoadAppointmentinfo(True, DateTime.Today, DateTime.Today)
                Pl.Dataset.Tables.Add(Pl.MetaDataTable)
                '
            ElseIf aExecQuery = DialogResult.Cancel Then
                'More to come...
            End If
        End If
        Return aExecQuery

    End Function

    Protected Overrides Sub LoadAppointmentinfo(InitEmpty As Boolean, startdate As Date, enddate As Date)
        If Pl IsNot Nothing AndAlso Pl.MetaDataTable IsNot Nothing AndAlso aSqlcmd IsNot Nothing Then
            SchedulerStorage1.BeginUpdate()
            Pl.MetaDataTable.Clear()
            'On Passe la sqcmd qui a les paramêtre de rentré
            Pl.MetaDataTable.GetMetaCommand()
            '
            aSqlcmd.Parameters("@Start").Value = startdate
            aSqlcmd.Parameters("@End").Value = enddate
            aSqlcmd.Parameters("@GroupId_FK").Value = IIf(InitEmpty, Guid.Empty, GroupID)
            aSqlcmd.Parameters("@ClassId_FK").Value = IIf(InitEmpty, Guid.Empty, ClassID)
            aSqlcmd.Parameters("@CategoryId_FK").Value = IIf(InitEmpty, Guid.Empty, CategID)
            aSqlcmd.Parameters("@TypeId_FK").Value = IIf(InitEmpty, Guid.Empty, TypeID)
            '
            Pl.MetaDataTable.FillResultQuery(SqlCmd:=aSqlcmd)
            SchedulerStorage1.EndUpdate()
        End If
    End Sub
End Class
